import 'package:flutter/material.dart';
import 'package:regular_assess/helper/utils/util.dart';
import 'package:regular_assess/pages/home/subs/new_page.dart';

class AppletController extends GetxController
    with GetSingleTickerProviderStateMixin {
  late AnimationController animCtrl;
  OverlayEntry? entry;
  var topY = 0.0.obs;

  @override
  void onInit() {
    animCtrl = AnimationController(
        vsync: this,
        lowerBound: 0,
        upperBound: 1.sh - 200.r,
        duration: const Duration(milliseconds: 300))
      ..addListener(() {
        topY.value = animCtrl.value;
      });
    super.onInit();
  }

  void create() {
    if (entry != null) {
      return;
    }
    entry ??= OverlayEntry(
      builder: (ctx) {
        return Obx(
          () => Positioned(
            left: 1.sw / 1.sh * topY.value,
            top: topY.value,
            width: 1.sw / 1.sh * (1.sh - topY.value),
            height: 1.sh - topY.value - 100.r,
            child: NewsPage(
              title: '初始化状态',
              isCircle: topY.value == animCtrl.upperBound,
            ),
          ),
        );
      },
    );
    Overlay.of(Get.context!).insert(entry!);
  }

  void show() {
    create();
    animCtrl.reverse();
  }

  void dismiss() {
    animCtrl.forward();
  }

  @override
  void dispose() {
    entry?.remove();
    entry = null;
    animCtrl.dispose();
    super.dispose();
  }
}
